package vip.specimen.system.result;

import io.swagger.annotations.ApiModelProperty;
import vip.specimen.system.common.enums.ResultCode;

/**
 * *@Author: 余浪人
 * *@Date：2021/2/4 14:50
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
public class JsonResult<T> {
    private static final long serialVersionUID = -1802122468331526708L;
    @ApiModelProperty(value = "状态码")
    private long status;
    @ApiModelProperty(value = "描述信息")
    private String msg;
    @ApiModelProperty(value = "数据 =>结果")
    private T data;

    public JsonResult() {
    }

    public JsonResult(long status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public JsonResult(long status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> JsonResult<T> success(T data) {
        return new JsonResult<T>(ResultCode.SUCCESS.getStatus(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     * @return
     */
    public static <T> JsonResult<T> success(T data, String message) {
        return new JsonResult<T>(ResultCode.SUCCESS.getStatus(), message, data);
    }

    public static <T> JsonResult<T> success() {
        return new JsonResult<T>(ResultCode.SUCCESS.getStatus(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> JsonResult<T> success(String message) {
        return new JsonResult<T>(ResultCode.SUCCESS.getStatus(), message, null);
    }

    /**
     * 错误返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> JsonResult<T> error(ResultCode errorCode) {
        return new JsonResult<T>(errorCode.getStatus(), errorCode.getMessage(), null);
    }

    public static <T> JsonResult<T> error(ResultCode errorCode, String msg) {
        return new JsonResult<T>(errorCode.getStatus(), msg, null);
    }

    public static <T> JsonResult<T> error(int errorCode, String msg) {
        return new JsonResult<T>(errorCode, msg, null);
    }

    public static <T> JsonResult<T> error(int errorCode, String msg, T data) {
        return new JsonResult<T>(errorCode, msg, data);
    }

    public static <T> JsonResult<T> error(ResultCode errorCode, T data) {
        return new JsonResult<T>(errorCode.getStatus(), errorCode.getMessage(), data);
    }

    /**
     * 失败
     *
     * @param errorCode 错误码
     * @param data      数据
     */
    public static <T> JsonResult<T> failed(ResultCode errorCode, T data) {
        return new JsonResult<T>(errorCode.getStatus(), errorCode.getMessage(), data);
    }

    public static <T> JsonResult<T> failed(ResultCode errorCode) {
        return new JsonResult<T>(errorCode.getStatus(), errorCode.getMessage(), null);
    }

    public long getStatus() {
        return status;
    }

    public void setStatus(long status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
